package org.example.day20240905.dao;

import org.example.day20240905.model.User;
import org.example.day20240905.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegisterDao {
    public User register(User user){
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from users where username = ? and password = ?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassWorld());
            ps.executeQuery();
            rs = ps.getResultSet();
            while(rs.next()){
                User userReturn = new User();
                userReturn.setId(rs.getString("id"));
                userReturn.setId(rs.getString("username"));
                userReturn.setId(rs.getString("password"));
                userReturn.setId(rs.getString("email"));
                return userReturn;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ps.close();
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
